var Mongoose = require('mongoose'),
    util = require('../helpers/util');

var commentSchema = Mongoose.Schema({
    user: {
        type: Mongoose.Schema.ObjectId,
        required: true,
        ref: 'User'
    },
    idea: {
        type: Mongoose.Schema.ObjectId,
        required: true,
        ref: 'Idea'
    },
    comment: {
        type: String
    }
}, {timestamps: true});

var Comment = Mongoose.model('Comment', commentSchema);

Comment.createNewComment = function (newComment, callback) {
    newComment.save(callback);
};

Comment.getIdeaCommentCount = function (ideaID, callback) {
    Comment.find({idea: new Mongoose.Types.ObjectId(ideaID)}).count(callback);
};

Comment.getCommentsByIdea = function (userId, ideaId, params, callback) {
    var skipItem = params.skipItem || 0,
        itemsperPage = params.itemsperPage || 6,
        sortParameter = params.sortParameter || 'createdAt',
        sortOrder = ((params.sortOrder === 'ascending') ? 1 : -1),
        sort = {},
        skip = parseInt(skipItem),
        limit = parseInt(itemsperPage);
    sort[sortParameter] = sortOrder;
    if (sortParameter != 'createdAt') {
        sort['createdAt'] = -1;
    }
    Comment.aggregate([
        {$match: {'idea': Mongoose.Types.ObjectId(ideaId)}},
        {
            "$project": {
                "_id": "$_id",
                "comment": "$comment",
                "user": "$user",
                "idea": "$idea",
                "createdAt": "$createdAt",
                "updatedAt": "$updatedAt"
            }
        },
        {"$sort": sort},
        {"$skip": skip},
        {"$limit": limit},
        {
            $lookup: {
                "from": "users",
                "localField": "user",
                "foreignField": "_id",
                "as": "users"
            }
        },
        {
            $unwind: "$users"
        },
        {
            "$project": {
                "_id": "$_id",
                "createdAt": "$createdAt",
                "updatedAt": "$updatedAt",
                "name": {$concat: ["$users.firstName", " ", {$ifNull: ["$users.lastName", " "]}]},
                "user": "$user",
                "idea": "$idea",
                "comment": "$comment",
                "isOwner": {$eq: ["$user", Mongoose.Types.ObjectId(userId)]}
            }
        }
    ], callback);
};

Comment.deleteCommentById = function (ideaId, commentId, callback) {
    Comment.find(
        {'_id': Mongoose.Types.ObjectId(commentId)},
        {'idea': Mongoose.Types.ObjectId(ideaId)}
    ).remove(callback);
};

Comment.deleteCommentsByIdea = function (ideaId, callback) {
    Comment.find(
        {'idea': Mongoose.Types.ObjectId(ideaId)}
    ).remove(callback);
};

Comment.recentCommentsByUser = function (userId, params, callback) {
    var inputFromDate = params.fromDate,
        toDate = params.toDate,
        fromDate = util.getDateWithoutTimeStamp(inputFromDate),
        todate1 = util.getDateWithoutTimeStamp(toDate),
        endDate = new Date(todate1.getTime() + (24 * 60 * 60 * 1000));
    Comment.find({createdAt: {$gte: fromDate, $lt: endDate}, user: Mongoose.Types.ObjectId(userId)}, callback);
};

Comment.getCommentsByUserId = function (userId, callback) {
    Comment.find({user: Mongoose.Types.ObjectId(userId)}, callback);
};

Comment.getAllCommentCount = function (callback) {
    Comment.find().count(callback);
};

Comment.getUserCommentCount = function (userId, callback) {
    Comment.find({user: Mongoose.Types.ObjectId(userId)}).count(callback);
};

module.exports = Comment;